package com.codebuffer.nowcoder;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

public class TreeDepth {

    public static int TreeDepth(TreeNode root) {

        if(null == root){
            return 0;
        }
        Queue<TreeNode> queue1 = new LinkedBlockingQueue();
        Queue<TreeNode> queue2 = new LinkedBlockingQueue();
        queue1.add(root);
        int i = 0;
        while(!queue1.isEmpty()||!queue2.isEmpty()){
            i++;
            if(!queue1.isEmpty()){
                depth(queue1,queue2);
            }else {
                depth(queue2,queue1);
            }

        }
        return i;
    }

    private static void depth(Queue<TreeNode> queue1, Queue<TreeNode> queue2) {
        while(!queue1.isEmpty()){
            TreeNode poll = queue1.poll();
            if(null != poll.left){
                queue2.add(poll.left);
            }
            if(null != poll.right){
                queue2.add(poll.right);
            }
        }
    }


    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(2);
        TreeNode treeNode3 = new TreeNode(3);
        treeNode1.left=treeNode2;
        treeNode1.right=treeNode3;
        TreeNode treeNode4 = new TreeNode(4);
        TreeNode treeNode5 = new TreeNode(5);
        treeNode2.left=treeNode4;
        treeNode2.right=treeNode5;
        TreeNode treeNode6 = new TreeNode(6);
        treeNode3.right = treeNode6;
        TreeNode treeNode7 = new TreeNode(7);

        treeNode5.left = treeNode7;

        System.out.println(TreeDepth(treeNode1));

    }
    static class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;

        public TreeNode(int val) {
            this.val = val;
        }
    }

}
